#include "TriMesh.h"
#include <QtOpenGL/QtOpenGL>
#include <gl/GLU.h>

void TriMesh::SetRenderingStyles(DrawType nStyles)
{
	m_nRenderingStyles = nStyles;
}

void TriMesh::DrawScene()
{
	int iRenderCount=0;

	// Different rendering style
	{
		// Drawing element
		if (m_nRenderingStyles == DRAW_POINTS)
		{
			DrawSceneByPoints();
			iRenderCount++;
		}
		else if (m_nRenderingStyles == DRAW_EDGES)
		{
			DrawSceneByEdges();
			iRenderCount++;
		}
		else if (m_nRenderingStyles == DRAW_TRIANGLES)
		{
			DrawSceneByTriangles();
			iRenderCount++;
		}
		
	}

	//ASSERT(iRenderCount==1);

	//// Show wire frame
	//if (m_nRenderingStyles & SHOW_WIRE_FRAME)
	//	ShowWireFrame();

	//// Show light position
	//if (m_nRenderingStyles & SHOW_LIGHTPOSITION)
	//	ShowLightPosition();

	// Show coordinate axis
	//if (m_nRenderingStyles & SHOW_COORDINATE)
	//	ShowCoordinate();

}

//void CTriMesh::GetBoundingBox(QVector3D &vMax, QVector3D &vMin)
//{
//	vMax = m_vMax;
//	vMin = m_vMin;
//}

void TriMesh::DrawSceneByPoints()
{
	//// Default color
	glColor3f (0.0f, 1.0f, 0.0f);
	//// Default material

	glPushAttrib(GL_ALL_ATTRIB_BITS);
	glPointSize(2.f);

	glBegin(GL_POINTS);
	for (int i = 0; i < vertices.size(); i ++)
	{
		glNormal3f(normals[i][0],normals[i][1],normals[i][2]);
		glVertex3f(vertices[i][0],vertices[i][1],vertices[i][2]);
		
	}
	glEnd();

	glPopAttrib();

}

void TriMesh::DrawSceneByEdges()
{
	int	i;

	glDisable(GL_TEXTURE_2D);

	// Default color
	glColor3f (0.0f, 0.0f, 1.0f);
	// Default material
	//SetMaterial();

	glBegin(GL_LINES);
	for (i = 0; i < faces.size(); i ++)
	{
		for(int j = 0; j < 3; j++)
		{
		   
			int n1 = faces[i][j];
			int n2 = faces[i][(j+1)%3];
			
			point &v1 = this->vertices[n1];
			point &v2 = this->vertices[n2];

			glNormal3f(normals[n1][0],normals[n1][1],normals[n1][2]);
			glVertex3f(v1[0], v1[1], v1[2]);

			glNormal3f(normals[n2][0],normals[n2][1],normals[n2][2]);
			glVertex3f(v2[0], v2[1], v2[2]);
		}
	}
	glEnd();

}

void TriMesh::DrawSceneByTriangles()
{
	//int	i, j, ntIdx, nCounter = 0;
	//QVector3D *v[3], *n[3];

	//SetMaterial();
	////glColor3f(0.3f, 0.3f, 0.3f);
	//for (j = 0; j < m_nTriangles; j ++)
	//{

	//	ntIdx = j;

	//	v[0] = &m_pVertices[m_pTriangles[ntIdx].m_nVertexIndices[0]].m_vPosition;
	//	v[1] = &m_pVertices[m_pTriangles[ntIdx].m_nVertexIndices[1]].m_vPosition;
	//	v[2] = &m_pVertices[m_pTriangles[ntIdx].m_nVertexIndices[2]].m_vPosition;

	//	//Draw Triangles
	//	glBegin(GL_TRIANGLES);
	//	{
	//		n[0] = &m_pTriangles[ntIdx].m_vN;
	//		if (n[0]->length() > 1.0e-10)
	//		{
	//			n[0] = &m_pVertices[m_pTriangles[ntIdx].m_nVertexIndices[0]].m_vNormal;
	//			n[1] = &m_pVertices[m_pTriangles[ntIdx].m_nVertexIndices[1]].m_vNormal;
	//			n[2] = &m_pVertices[m_pTriangles[ntIdx].m_nVertexIndices[2]].m_vNormal;


	//			glColor3f(n[0]->x()*0.5f+0.5f, n[0]->y()*0.5f+0.5f, n[0]->z()*0.5f+0.5f);
	//			glNormal3f(n[0]->x(), n[0]->y(), n[0]->z());
	//			glVertex3f(v[0]->x(), v[0]->y(), v[0]->z());

	//			glColor3f(n[1]->x()*0.5f+0.5f, n[1]->y()*0.5f+0.5f, n[1]->z()*0.5f+0.5f);
	//			glNormal3f(n[1]->x(), n[1]->y(), n[1]->z());
	//			glVertex3f(v[1]->x(), v[1]->y(), v[1]->z());

	//			glColor3f(n[2]->x()*0.5f+0.5f, n[2]->y()*0.5f+0.5f, n[2]->z()*0.5f+0.5f);
	//			glNormal3f(n[2]->x(), n[2]->y(), n[2]->z());
	//			glVertex3f(v[2]->x(), v[2]->y(), v[2]->z());
	//		}
	//		else
	//		{
	//			glNormal3f(n[0]->x(), n[0]->y(), n[0]->z());
	//			glVertex3f(v[0]->x(), v[0]->y(), v[0]->z());
	//			glVertex3f(v[1]->x(), v[1]->y(), v[1]->z());
	//			glVertex3f(v[2]->x(), v[2]->y(), v[2]->z());
	//		}
	//	glEnd();
	//	}
	//		
	//}

	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	//glDisable(GL_CULL_FACE);
}
